package com.tebiecloud.yunst.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.yunst.client.model.entity.YunRefund;
import com.tebiecloud.yunst.server.service.YunRefundService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 云商通退款表 前端控制器
 * @author tebie
 * @date 2019-09-07
 */
@RestController
@RequestMapping("/yunrefund")
public class YunRefundController {

    @Autowired
    private YunRefundService yunRefundService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<YunRefund>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        YunRefund query = pageParams.mapToObject(YunRefund.class);
        QueryWrapper<YunRefund> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(yunRefundService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<YunRefund> get(@RequestParam("id") Long id) {
        YunRefund entity = yunRefundService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param bizUserId 通联用户id,
     * @param bizOrderNo 退款单号,
     * @param orgBizOrderNo 源订单号,
     * @param amount 金额（单位：分）,
     * @param payStatus 支付状态：0退款中，1已退款，2退款失败,
     * @param remark 
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "bizUserId", required = false) String bizUserId,
        @RequestParam(value = "bizOrderNo", required = false) String bizOrderNo,
        @RequestParam(value = "orgBizOrderNo", required = false) String orgBizOrderNo,
        @RequestParam(value = "amount", required = false) Integer amount,
        @RequestParam(value = "payStatus", required = false) Integer payStatus,
        @RequestParam(value = "remark", required = false) String remark
    ) {
        YunRefund entity = new YunRefund();
        entity.setBizUserId(bizUserId);
        entity.setBizOrderNo(bizOrderNo);
        entity.setOrgBizOrderNo(orgBizOrderNo);
        entity.setAmount(amount);
        entity.setPayStatus(payStatus);
        entity.setRemark(remark);
        yunRefundService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param bizUserId 通联用户id
     * @param bizOrderNo 退款单号
     * @param orgBizOrderNo 源订单号
     * @param amount 金额（单位：分）
     * @param payStatus 支付状态：0退款中，1已退款，2退款失败
     * @param remark 
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "yunRefundId", required = false) Long yunRefundId,
            @RequestParam(value = "bizUserId", required = false) String bizUserId,
            @RequestParam(value = "bizOrderNo", required = false) String bizOrderNo,
            @RequestParam(value = "orgBizOrderNo", required = false) String orgBizOrderNo,
            @RequestParam(value = "amount", required = false) Integer amount,
            @RequestParam(value = "payStatus", required = false) Integer payStatus,
            @RequestParam(value = "remark", required = false) String remark
    ) {
        YunRefund entity = new YunRefund();
        entity.setYunRefundId(yunRefundId);
        entity.setBizUserId(bizUserId);
        entity.setBizOrderNo(bizOrderNo);
        entity.setOrgBizOrderNo(orgBizOrderNo);
        entity.setAmount(amount);
        entity.setPayStatus(payStatus);
        entity.setRemark(remark);
        yunRefundService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            yunRefundService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            yunRefundService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

//    @PostMapping("refund")
//    public ResultBody refund(
//            @RequestParam(value = "bizUserId", required = false) String bizUserId,
//            @RequestParam(value = "oriBizOrderNo", required = false) String oriBizOrderNo,
//            @RequestParam(value = "amount", required = false) Long amount
//    ) {
//
//        yunRefundService.refund(bizUserId,oriBizOrderNo,amount);
//        return ResultBodyUtils.ok();
//    }

}
